package com.shenzj.wc

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.api.scala._

// 流处理WordCount
object StreamWordCount {
  def main(args: Array[String]): Unit = {
    // 创建流处理的执行环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    // 接受一个socket文本流
    val inputDataStream = env.socketTextStream("192.168.188.128",7777)

    // 进行转化处理统计
    val resultDataStream: DataStream[(String, Int)] = inputDataStream
      .flatMap(_.split(" ")) /*等价于 flatMap(x => x.split(" "))*/
      .filter(_.nonEmpty)
      .map((_,1)) /*map成一个二元组*/
      .keyBy(0)
      .sum(1)


    // 打印输出
    resultDataStream.print()

    // 启动任务执行
    env.execute("stream word count")
  }
}
